define(["bbcare"], function ($bb) {
  var vm, event = {}, URL = $bb.baseUrl('followup')

  event.init = function () {
    vm = undefined
    createVm()
  }

  function createVm () {
    vm = new Vue({
      el: '#wrapper',
      data: {
        keyword: '',
        hospitalNo: '',
        bedNo: '',
        hospitalName: '全部',
        status: '全部',
        table: {
          loading: false,
          data: [{ No: 1 }],
          total: 0,
          current: 1,
          size: 10
        },
        dialog: {
          show: false,
          loading: false,
          data: {
            status: '',
            diagnosis: '',
            date: '',
            remark: ''
          }
        },
        hospitalNames: [],
        type: '',
        currentPatient: {}
      },
      watch: {
        type: function (val) {
          console.log(val)
        }
      },
      created: function () {
        this.getTableData()
        this.getHospitalName()
      },
      methods: {
        /**
         * 搜索
         */
        handleSearch: function () {
          this.getTableData()
        },
        handleCancel: function () {
          this.dialog.show = false
        },
        handleToDetail: function (row) {
          if (row.gender === 'N' || row.gender == 1) {
            imgName = 'boy'
          } else {
            imgName = 'girl'
          }
          var imgSrc = "page/followup/imgs/comm/" + imgName + ".png"
          var params = {
            patientSchemeId: row.ptSchemeid,
            followQueueId: row.queueId,
            patientId: row.id,
            patientName: row.name,
            imgSrc: imgSrc,
            patientId: row.id,
          }
          $bb.ajax({
            url: URL + 'followup/service/patient/patientInfoQueryByPtId',
            method: 'post',
            data: JSON.stringify({
              data: {
                patientId: row.id,
                ptSchemeId: row.ptSchemeid
              }
            }),
            success: function (res) {
              var queueName = res.data.QueueSchemeInfo.queueName
              params.queuename = queueName
              $bb.openPage('outPatient', params)
            }
          })
        },
        /**
         * 改变页码
         */
        handleChangePage: function (num) {
          this.table.current = num
          this.getTableData()
        },
        /**
         * 改变当前页大小
         * @param {*} num 
         */
        handleSizeChange: function (num) {
          this.table.size = num
          this.getTableData()
        },
        /**
         * 获取医院名称
         */
        getHospitalName: function () {
          var that = this
          $bb.ajax({
            url: URL + 'followup/service/patient/hospitalList',
            method: 'get',
            data: JSON.stringify(''),
            success: function (res) {
              if (res.success) {
                that.hospitalNames = res.data
              } else {
                that.$message.error(res.msg)
              }
            }
          })
        },
        handleShowDialog: function (row) {
          this.dialog.data.status = row.status
          this.dialog.data.date = row.outHospitalTime
          this.currentPatient = row
          this.dialog.show = true
        },
        handleSubmit: function () {
          // 1. 出院 或 转入 都需要检测是否填写信息 和 日期
          var resultText = ''
          if (this.dialog.data.status == '在院') {
            // if (!this.dialog.data.diagnosis) resultText = '请填写出院诊断'
            // if (!this.dialog.data.date) resultText = '请选择出院时间'
          } else if (this.dialog.data.status == '出院') {
            if (!this.dialog.data.diagnosis) resultText = '请填写出院诊断'
            if (!this.dialog.data.date) resultText = '请选择出院时间'
          } else if (this.dialog.data.status == '转到其他病区') {
            if (!this.dialog.data.remark) resultText = '请填写转入备注'
            if (!this.dialog.data.date) resultText = '请选择出院时间'
          }
          if (resultText !== '') return this.$message.error(resultText)

          if (!this.dialog.data.date) return this.$message.error('请填写')
          var params = {
            userId: this.currentPatient.id,
            status: this.dialog.data.status,
            outHospitalRemark: this.dialog.data.diagnosis,
            transferHospitalRemark: this.dialog.data.remark,
            outHospitalTime: this.getFormatTime(this.dialog.data.date),
            transferHospitalTime: this.getFormatTime(this.dialog.data.date)
          }
          if (this.dialog.data.status == '转到其他病区' || this.dialog.data.status == '在院') {
            delete params.outHospitalRemark
            delete params.outHospitalTime
          }
          if (this.dialog.data.status == '出院' || this.dialog.data.status == '在院') {
            delete params.transferHospitalRemark
            delete params.transferHospitalTime
          }

          var that = this
          that.dialog.loading = true
          $bb.ajax({
            url: URL + 'followup/service/patient/updatePatientStatus',
            data: JSON.stringify(params),
            success: function (res) {
              if (res.success) {
                that.$message.success(res.data)
                that.dialog.loading = false
                that.dialog.show = false
                that.getTableData()
              } else {
                that.$message.error(res.msg)
              }
              that.dialog.loading = false
            }
          })
          that.currentPatient = null
        },
        getFormatTime: function (date) {
          if (typeof date === 'string') date = new Date(date)
          var year = date.getFullYear(),
            month = date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1,
            day = date.getDate() < 10 ? '0' + date.getDate() : date.getDate()
          return year + '-' + month + '-' + day
        },
        getTableData: function () {
          this.table.loading = true
          var params = {
            currentPage: this.table.current,
            pageSize: this.table.size,
            userName: this.keyword,
            inPatientNo: this.hospitalNo,
            hospitalBedNo: this.bedNo,
            hospitalName: this.hospitalName == '全部' ? '' : this.hospitalName,
            status: this.status == '全部' ? '' : this.status
          }
          var that = this
          $bb.ajax({
            url: URL + 'followup/service/patient/patientListPage',
            data: JSON.stringify(params),
            success: function (res) {
              if (res.success) {
                that.table.total = res.data.count
                res.data.data.forEach(function (item, index) {
                  item.No = (that.table.current - 1) * that.table.size + index + 1
                })
                that.table.data = res.data.data
                that.type = JSON.parse(JSON.stringify(that.status))
              } else {
                that.$message.error(res.msg)
              }
              that.table.loading = false
            }
          })
        }
      }
    })
  }

  return event
})